//
//  TransferInfoCell.m
//  PigMarket
//
//  Created by mac on 2019/5/8.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "TransferInfoCell.h"

@interface TransferInfoCell ()

@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UILabel *moneyTips;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UIButton *rightBtn;


@end
@implementation TransferInfoCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    TransferInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[TransferInfoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
        [self initAction];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.title];
    [self.contentView addSubview:self.rightBtn];
    [self.contentView addSubview:self.icon];
    [self.contentView addSubview:self.inputField];
    [self.contentView addSubview:self.moneyTips];
    [self.contentView addSubview:self.verifyCodeBtn];
}

- (void)initLayout {
    // 布局子控件
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(22);
    }];
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
    [self.inputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.left.mas_equalTo(self.icon.mas_right).offset(8);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(150);
    }];
    [self.moneyTips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.top.mas_equalTo(17);
        make.right.mas_equalTo(-15);
    }];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(24);
    }];
    [self.verifyCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(94, 30));
    }];
}
- (void) initAction {

    
    [self.verifyCodeBtn addTarget:self action:@selector(verifyCodeTapped) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)verifyCodeTapped {
    if (self.getVerifyCodeBlock) {
        self.getVerifyCodeBlock();
    }
}
#pragma mark - getter and setter
- (void)setInfoDic:(NSDictionary *)infoDic {
    NSString *title = infoDic[@"title"];
    NSString *rightText = infoDic[@"rightText"];
    NSString *iconStr = infoDic[@"icon"];
    NSString *text = infoDic[@"text"];
    
    if (![JTool iSNull:title]) {//内容项
        if ([title isEqualToString:@"此猪共花费:"]) {
            //共花费金钱数
            self.title.textColor = [UIColor colorWithHexString:@"#999999"];
         }else {
            self.title.textColor = [UIColor textBlackColor];
        }
        self.title.text = title;
    } else {//标题项
        self.title.hidden = YES;
    }
    
    if (![JTool iSNull:iconStr] || [title rangeOfString:@"接收人"].location != NSNotFound) {//判断icon和输入框
        self.icon.image = [UIImage imageNamed:iconStr];
        self.inputField.placeholder = text;
    } else {
        self.inputField.hidden = YES;
        self.icon.hidden = YES;
    }
    
    if ([text rangeOfString:@"说明"].location != NSNotFound) {
        self.moneyTips.text = text;
    } else {
        self.moneyTips.hidden = YES;
    }
    if ([rightText rangeOfString:@"获取验证码"].location == NSNotFound) {
        self.verifyCodeBtn.hidden = YES;

    } else {
        self.verifyCodeBtn.hidden = NO;

    }
   if ([rightText rangeOfString:@"adopted_transfer_addressbook"].location != NSNotFound){
        [self.rightBtn setImage:[UIImage imageNamed:rightText] forState:UIControlStateNormal];
      
    } else if ([rightText rangeOfString:@"￥"].location != NSNotFound){
        [self.rightBtn setTitle:rightText forState:UIControlStateNormal];
    } else {
        self.rightBtn.hidden = YES;
    }
}
- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.font = [UIFont systemFontOfSize:15];
    }
    return _title;
}
- (UILabel *)moneyTips {
    if (!_moneyTips) {
        _moneyTips = [[UILabel alloc] init];
        _moneyTips.textColor = [UIColor colorWithHexString:@"#999999"];
        _moneyTips.font = [UIFont smallFont];
    }
    return _moneyTips;
}
- (UIImageView *)icon {
    if (!_icon) {
        _icon = [[UIImageView alloc] init];
    }
    return _icon;
}
- (UITextField *)inputField {
    if (!_inputField) {
        _inputField = [[UITextField alloc] init];
        [_inputField setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
        _inputField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    return _inputField;
}
- (UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.rightBtn setTitleColor:[UIColor colorWithHexString:@"#F25C62"] forState:UIControlStateNormal];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    }
    return _rightBtn;
}
- (VerifyCodeButton *)verifyCodeBtn {
    if (!_verifyCodeBtn) {
        _verifyCodeBtn = [VerifyCodeButton buttonWithType:UIButtonTypeCustom];
        _verifyCodeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _verifyCodeBtn.layer.cornerRadius = 15;
        _verifyCodeBtn.layer.borderColor = [UIColor primaryColor].CGColor;
        _verifyCodeBtn.layer.borderWidth = 1;
        [_verifyCodeBtn addTarget:self action:@selector(verifyCodeTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _verifyCodeBtn;
}
@end
